package Database;

import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;

import Logger.AppLogger;
import Objects.PendingMessage;
import Objects.UserProfile;

public class Database 
{
	//***************************************Class Variables*******************************************//

	private static Database _database = null;


	//Actual data:
	private String _authToken;
	private Vector<UserProfile> _usersProfiles;
	private Vector<PendingMessage> _pendingMessages;

	//***************************************Constructors*******************************************//


	private Database()
	{
		this._authToken = null;
		this._usersProfiles = new Vector<UserProfile>();
		this._pendingMessages = new Vector<PendingMessage>();

/*		//not in range of israel
		UserProfile u1 = new UserProfile("bla1", "bla1", "bla1");
		u1.updatePosition(29.0, 37.13);
		UserProfile u2 = new UserProfile("bla2", "bla2", "bla2");
		u2.updatePosition(29.08, 33.24);
		//yes in range of israel
		UserProfile u3 = new UserProfile("bla3", "bla3", "bla3");
		u3.updatePosition(33.40, 34.90);
		UserProfile u4 = new UserProfile("bla4", "bla4", "bla4");
		u4.updatePosition(29.90, 35.30);
		
		
		this._usersProfiles.add(u1);
		this._usersProfiles.add(u2);
		this._usersProfiles.add(u3);
		this._usersProfiles.add(u4);
*/	}


	public static Database getInstance()
	{
		if(_database == null)
		{
			_database = new Database();
		}

		return _database;
	}


	//***************************************Public Methods*******************************************//


	//***********************AuthToken related Methods************************//


	public void saveAuthToken(String authToken) 
	{
		this._authToken = authToken;
	}


	public String getAuthToken() 
	{
		return this._authToken;
	}


	//***********************Users related Methods************************//


	public void addUserProfile(UserProfile newUser)
	{
		this._usersProfiles.add(newUser);
	}


	public UserProfile removeUserProfile(String android_id)
	{
		for (Iterator<UserProfile> iterator = this._usersProfiles.iterator(); iterator.hasNext();) 
		{
			UserProfile user = (UserProfile) iterator.next();
			if(user.get_android_id().equals(android_id))
			{
				iterator.remove();
				return user;
			}
		}
		return null;
	}


	public String getC2DMRegistrationId(String android_id)
	{
		for (Iterator<UserProfile> iterator = this._usersProfiles.iterator(); iterator.hasNext();) 
		{
			UserProfile user = (UserProfile) iterator.next();
			if(user.get_android_id().equals(android_id))
				return user.get_c2dm_register_id();
		}

		return null;
	}
	

	public void addPendingMessage(PendingMessage newMessage)
	{
		this._pendingMessages.add(newMessage);
	}

	//now this function is not that efficient but later with a real database would be very quick!
	public PendingMessage removePendingMessage(int message_id)
	{
		for (Iterator<PendingMessage> iterator = this._pendingMessages.iterator(); iterator.hasNext();) 
		{
			PendingMessage message = (PendingMessage) iterator.next();
			if(message.get_message_id() == message_id)
			{
				iterator.remove();
				return message;
			}
		}
		//no message with that id was waiting
		return null;
	}


	public PendingMessage getPendingMessage(int message_id)
	{
		for (Iterator<PendingMessage> iterator = this._pendingMessages.iterator(); iterator.hasNext();) 
		{
			PendingMessage message = (PendingMessage) iterator.next();
			if(message.get_message_id() == message_id)
			{
				return message;
			}
		}
		//no message with that id was waiting
		return null;
	}


	public UserProfile getUserProfile(String my_android_id) 
	{
		for (Iterator<UserProfile> iterator = this._usersProfiles.iterator(); iterator.hasNext();) 
		{
			UserProfile userProf = (UserProfile) iterator.next();
			if(userProf.get_android_id().equals(my_android_id))
			{
				return userProf;
			}
		}

		return null;
	}

	public Vector<UserProfile> getAllUsers() 
	{
		return this._usersProfiles; 
	}


	public void updateUserDetails(String android_id, String c2dm_register_id, String google_account) {
		for (Iterator<UserProfile> iterator = this._usersProfiles.iterator(); iterator.hasNext();) 
		{
			UserProfile userProf = (UserProfile) iterator.next();
			if(userProf.get_android_id().equals(android_id))
			{
				userProf.set_c2dm_register_id(c2dm_register_id);
				userProf.set_google_account(google_account);
			}
		}
	}

	public Vector<UserProfile> getUsersInLocation(Double x1, Double y1, 
			Double x2, Double y2) {

		Double q = x1+(x2/2);
		Double w = y1+(y2/2);
		System.out.println("getUsersInLocation = ("+x1+","+y1+"),("+x2+","+y2+")");
		Vector<UserProfile> usersInZone = new Vector<UserProfile>();
		if(x1==0 & y1==0 & x2==0 & y2==0 ){ //returning all the users
			usersInZone = this._usersProfiles;
		}
		else{
			for (Iterator<UserProfile> iterator = this._usersProfiles.iterator(); iterator.hasNext();) 
			{
				UserProfile userProf = (UserProfile) iterator.next();
				if (userProf.getX()!=500 || userProf.getY()!=500) 
					//x=500 & y=500 are initials values for users that didn't send location
				/*if(userProf.getX() > x2 && userProf.getX() < x1 &&
						userProf.getY() >= y1 && userProf.getY() <= y2)*/
				{
					usersInZone.add(userProf);
					System.out.println("adding "+userProf.get_android_id());
				}
//				else
//					System.out.println("not adding "+userProf.get_android_id());
			}
		}
		return usersInZone;
	}

	//	public PendingMessage removeRegistration(String android_id)

}
